package util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;

import fire.Main;
import fire.chacracter.Player;
import fire.npc.NPC;
import fire.room.Map;
import fire.room.TerrainObject;

import jgame.JGObject;

public class OOMLoad {
	public static void oomLoad(String oomFile, ArrayList<JGObject> oomList,
			Main main) {
		int ln = 0;
		try {
			URL url = new URL(main.getCodeBase(), oomFile);
			InputStreamReader ins = new InputStreamReader(url.openStream());
			BufferedReader in = new BufferedReader(ins);
			String line;
			String[] fields;
			try {
				while ((line = in.readLine()) != null) {
					fields = line.split("\t");
					if (fields[0].equalsIgnoreCase("terrainobject")) {
						oomTerrainLoad(fields, oomList);
					}
					if (fields[0].equalsIgnoreCase("enemy")) {
						oomEnemyLoad(fields, oomList);
					}
					if (fields[0].equalsIgnoreCase("npc")) {
						oomNpcLoad(fields, oomList);
					}
					if (fields[0].equalsIgnoreCase("mapsize")) {
						oomSetSize(fields, main);
					}
					if (fields[0].equalsIgnoreCase("player")) {
						oomSetPlayer(fields, main);
					}
					ln++;
				}
			} catch (IOException e) {
				System.err.println("error format in line " + ln + " in file "
						+ oomFile);
			}
		} catch (Exception e) {
			System.err.println(oomFile + "  can't be found");
		}
	}

	private static void oomSetPlayer(String[] fields, Main main) {
		Player player = main.getPlayer();
		if (player == null) {
			main.setPlayer(new Player("player", -500, -500, null, 0));
		}
		player = main.getPlayer();
		player.setPos(Double.parseDouble(fields[1]),
				Double.parseDouble(fields[2]));
		player.setGraphic(fields[3]);
		player.setSpeed(Double.parseDouble(fields[4]));
	}

	private static void oomSetSize(String[] fields, Main main) {
		main.setPFSize(Integer.parseInt(fields[1]), Integer.parseInt(fields[2]));
	}

	private static void oomNpcLoad(String[] fields, ArrayList<JGObject> oomList) {
		oomList.add(new NPC(fields[1], Boolean.parseBoolean(fields[2]), Double
				.parseDouble(fields[3]), Double.parseDouble(fields[4]), Integer
				.parseInt(fields[5]), fields[6]));
	}

	private static void oomEnemyLoad(String[] fields,
			ArrayList<JGObject> oomList) {
	}

	private static void oomTerrainLoad(String[] fields,
			ArrayList<JGObject> oomList) {
		if (fields.length == 7)
			oomList.add(new TerrainObject(fields[1], Boolean
					.parseBoolean(fields[2]), Double.parseDouble(fields[3]),
					Double.parseDouble(fields[4]), Integer.parseInt(fields[5]),
					fields[6]));
		else
			oomList.add(new TerrainObject(fields[1], Boolean
					.parseBoolean(fields[2]), Double.parseDouble(fields[3]),
					Double.parseDouble(fields[4]), Integer.parseInt(fields[5]),
					fields[6], Integer.parseInt(fields[7]), Integer
							.parseInt(fields[8]), Integer.parseInt(fields[9]),
					Integer.parseInt(fields[10])));
	}
}
